<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        //flat方法就是要给一个深度然后知道将数组扁平化多少层后就返回
        Array.prototype.myFlat = function(deepth=1)
        {
            let result = [];
            function myflat(arr,deepth)
            {
                let result = [];
                for(let i = 0;i<arr.length;i++)
                {
                    if(Array.isArray(arr[i])&&deepth>0)
                    {
                        result.push(...myflat(arr[i],deepth-1));
                    }
                    else
                    {
                        result.push(arr[i]);
                    }
                }

                return result;
            }
            
           result = myflat(this,deepth);

            return result;
        }
        let arr = [12,3,4,[1,2,3,[3,4,5]]];
        let newArr = arr.myFlat(1);
        console.log(newArr);
    </script>
</body>
</html>